<template>
  <div style="padding: 10px">
    <el-card>
      <div style="display: flex; justify-content: space-between">
        <div>
          <el-form style="width: 100%; display: flex">
            <el-form-item label="学科名称" label-width="80px">
              <el-input size="small" v-model="list.subjectName"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="clear" size="small" style="margin: 0 10px"
                >清除</el-button
              >
            </el-form-item>
            <el-form-item>
              <el-button type="primary" size="small" @click="search"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div>
          <el-button
            icon="el-icon-edit"
            type="success"
            size="small"
            @click="addd"
            >新增学科</el-button
          >
        </div>
      </div>
      <el-alert :closable="false" :title="subList.counts" type="info" show-icon>
      </el-alert>
      <el-table
        size="small"
        :data="subList.items"
        style="width: 100%; margin-top: 15px; font-size: 13px"
      >
        <el-table-column type="index" label="序号" width="48">
        </el-table-column>
        <el-table-column prop="subjectName" label="学科名称" width="180">
        </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="160">
        </el-table-column>
        <el-table-column label="前台是否显示">
          <template #default="scope">
            {{ scope.row.isFrontDisplay === 1 ? "是" : "否" }}
          </template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录">
        </el-table-column>
        <el-table-column prop="tags" label="标签"> </el-table-column>
        <el-table-column prop="totals" label="题目数量"> </el-table-column>
        <el-table-column label="操作" width="240">
          <template #default="scope">
            <div style="display: flex; justify-content: space-between">
              <el-button
                @click="
                  $router.push(
                    `directorys?id=${scope.row.id}&name=${scope.row.subjectName}`
                  )
                "
                type="text"
                >学科分类</el-button
              >
              <el-button
                @click="
                  $router.push(
                    `tags?id=${scope.row.id}&name=${scope.row.subjectName}`
                  )
                "
                type="text"
                >学科标签</el-button
              >
              <el-button @click="amend(scope.row)" type="text">修改</el-button>
              <el-button @click="del(scope.row)" type="text">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="bottom">
        <el-pagination
          @size-change="handleSizeChange($event)"
          @current-change="handleCurrentChange($event)"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="10"
          layout=" prev, pager, next, sizes, jumper"
          :background="true"
          :page-count="subList.pages"
        >
        </el-pagination>
      </div>
    </el-card>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="21%"
      :before-close="handleClose"
    >
      <el-form :model="addSubject" :rules="rules" ref="dialogFormRef">
        <el-form-item label="学科名称" label-width="80px" prop="subjectName">
          <el-input
            size="small"
            placeholder="请输入学科名称"
            v-model="addSubject.subjectName"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否显示" label-width="80px">
          <el-switch
            v-model="addSubject.isFrontDisplay"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="edit">取 消</el-button>
        <el-button type="primary" @click="addSubjects">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, add, update, remove } from '@/api/hmmm/subjects'
export default {
  data () {
    return {
      addSubject: {
        isFrontDisplay: true,
        id: null,
        subjectName: ''
      },
      title: '',
      dialogVisible: false,
      list: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      },
      subList: {},
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async del (row) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove(row)
          this.getList(this.list)
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        })
        .catch(() => {})
    },
    amend (row) {
      this.title = '修改学科'
      this.addSubject.subjectName = row.subjectName
      this.addSubject.id = row.id
      this.addSubject.isFrontDisplay = row.isFrontDisplay === 1
      this.dialogVisible = true
    },
    edit () {
      this.addSubject.subjectName = ''
      this.addSubject.isFrontDisplay = true
      this.addSubject.id = null
      this.dialogVisible = false
    },
    handleClose () {
      this.addSubject.subjectName = ''
      this.addSubject.isFrontDisplay = true
      this.addSubject.id = null
      this.dialogVisible = false
    },
    addd () {
      this.title = '新增学科'
      this.dialogVisible = true
    },
    async addSubjects () {
      if (this.title === '新增学科') {
        await this.$refs.dialogFormRef.validate()
        this.addSubject.isFrontDisplay =
          this.addSubject.isFrontDisplay === true ? 1 : 0
        await add(this.addSubject)
        this.getList(this.list)
        this.addSubject.subjectName = ''
        this.addSubject.isFrontDisplay = true
        this.dialogVisible = false
      } else {
        await update(this.addSubject)
        this.getList(this.list)
        this.addSubject.subjectName = ''
        this.addSubject.isFrontDisplay = true
        this.addSubject.id = null
        this.dialogVisible = false
      }
    },
    clear () {
      this.list.subjectName = ''
      this.list.page = 1
      this.list.pagesize = 10
    },
    search () {
      this.list.page = 1
      this.list.pagesize = 10
      this.getList(this.list)
    },
    async getList (lists) {
      const { data: res } = await list(lists)
      this.subList = res
      this.subList.counts = `数据一共 ${this.subList.counts} 条`
    },
    async handleSizeChange (e) {
      this.list.pagesize = e
      this.getList(this.list)
    },
    async handleCurrentChange (e) {
      this.list.page = e
      this.getList(this.list)
    }
  },
  async created () {
    this.getList(this.list)
  }
}
</script>

<style scoped lang='scss'>
::v-deep .has-gutter {
  th {
    background-color: #fafafa;
  }
}
::v-deep .el-alert__icon {
  font-size: 18px;
}
::v-deep .el-table__header{
  border-bottom: 1px solid #e8e8e8;
}
.bottom {
  width: 100%;
  text-align: right;
  margin-top: 20px;
}
</style>
